<template>
    <div>
        <div style="display: flex;align-items: center;justify-content: center">
            <el-input style="width: 150px" placeholder="查询名称" v-model="title" clearable></el-input>
            <el-select style="width: 100px; margin: 0 10px" v-model="sign" placeholder="竞赛类型">
                <el-option
                        v-for="item in options"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-select style="width: 150px; margin: 0 10px" v-model="teacher" placeholder="指导教师">
                <el-option
                        v-for="item in teachers"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>

            <el-button type="primary" style="margin-left: 10px" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" plain>重置</el-button>
        </div>
        <el-table :data="tableData"
                  stripe
                  :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666'}"
                  style="font-size: 14px; margin-top: 20px"
        >
            <el-table-column width="55" align="center" ></el-table-column>
            <el-table-column prop="title" label="竞赛名称" align="center"></el-table-column>
            <el-table-column prop="sign" label="竞赛类型" align="center"></el-table-column>
            <el-table-column prop="username" label="参赛者" align="center"></el-table-column>
            <el-table-column prop="major" label="学部" align="center"></el-table-column>
            <el-table-column prop="opus" v-slot="scope" label="参赛作品" align="center">
                <el-button type="primary" plain @click="downloadFile(scope.row)"
                >下载</el-button>
            </el-table-column>
            <el-table-column v-slot="scope" prop="payfor" label="支付状态" align="center">
                <el-tag type="primary" style="font-size: 14px" v-if="scope.row.pay === '免费'">无需付费</el-tag>
                <el-tag type="warning" style="font-size: 14px"
                        v-if="scope.row.payfor === '待支付' && scope.row.pay === '付费'">待支付
                </el-tag>
                <el-tag type="success" style="font-size: 14px"
                        v-if="scope.row.payfor === '已支付' && scope.row.pay === '付费'">已支付
                </el-tag>
            </el-table-column>
            <el-table-column v-slot="scope" prop="audit" label="审核状态" align="center">
                <el-tag type="primary" style="font-size: 14px"
                        v-if="scope.row.audit === '待审核'">待审核</el-tag>
                <el-tag type="success" style="font-size: 14px"
                        v-if="scope.row.audit === '已通过'">已通过
                </el-tag>
                <el-tag type="warning" style="font-size: 14px"
                        v-if="scope.row.audit === '未通过'">未通过
                </el-tag>
            </el-table-column>
<!--            <el-table-column prop="content" label="审核内容" align="center" show-overflow-tooltip></el-table-column>-->
            <el-table-column label="操作" align="center" width="220" v-if="user.role === '管理员'
                    || user.role === '教师'">
                <template v-slot="scope" style="font-size: 14px">
                    <el-button style="padding: 6px" size="mini" type="success" plain @click="$router.push('/enrollDetail?id=' + scope.row.id)">详情
                    </el-button>
                    <el-button style="padding: 6px" size="mini" type="warning" plain @click="handleAdd(scope.row)">评分
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0; display: flex; align-items: center; justify-content: center">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="total">
            </el-pagination>
        </div>
        <!--        弹窗-->
        <el-dialog title="成绩评分" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false">
            <el-form :model="grade" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
                <el-form-item label="参赛者id" prop="partid">
                    <el-input disabled v-model="enroll.partid"></el-input>
                </el-form-item>
                <el-form-item label="报名id" prop="enrollid">
                    <el-input disabled v-model="enroll.id"></el-input>
                </el-form-item>
                <el-form-item label="分数" prop="score">
                    <el-input-number
                            v-model="grade.score"
                            :min="0"
                            :max="100"
                            controls-position="right"
                            :step="10"
                    >
                    </el-input-number>
                </el-form-item>
                <el-form-item label="评价" prop="assess">
                    <el-input type="textarea" maxlength="500" :autosize="{ minRows: 4}"
                              show-word-limit v-model="grade.assess" placeholder="评价"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="fromVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "SetGrade",
        data() {
            return {
                tableData: [], //所有的数据
                pageNum: 1, //当前的页码
                pageSize: 6, //每页显示的个数
                title: '',
                sign: '',
                teacher: '',
                //竞赛类型
                options: [],
                //教师
                teachers: [],
                //审核
                audits: [{
                    value: 'true',
                    label: '审核通过'
                }, {
                    value: 'false',
                    label: '审核未通过'
                }],
                total: 0, //默认是0
                fromVisible: false,
                grade: {},
                enroll:{},
                user: JSON.parse(localStorage.getItem("honey-user") || '{}'),//拿取个人用户信息
                assess: '',
                rules: {
                    score: [
                        {required: true, message: '请填写分数', trigger: 'blur'}
                    ],
                },
            }
        },
        created() {
            this.load()
        },
        methods: {
            handleAdd(row) { //编辑
                this.$request.get('/enroll/selectById/' + row.id).then(res => {
                    this.enroll = res.data
                })
                // this.$request.get('/grade/selectById/' + row.id).then(res => {
                //     this.grade = res.data
                // })
                this.fromVisible = true //打开新增弹窗
            },
            downloadFile(row){
                window.open(row.opus)
            },
            save() { //保存
                this.$refs.formRef.validate((valid) => {
                    if (valid) { //验证通过
                        //添加当前的报名信息到数据库
                        // this.manualUpload()
                        this.grade.partid = this.enroll.partid
                        this.grade.enrollid = this.enroll.id
                        this.$request.post('/grade/add', this.grade).then(res => {
                            if (res.code === '200') {
                                //更新成功
                                this.$message.success('保存成功')
                                this.load(1)
                                this.fromVisible = false
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                })
            },
            reset() {
                this.title = ''
                this.sign = ''
                this.teacher = ''
                this.load()
            },
            load(pageNum) { //分页查询
                if (pageNum) {
                    this.pageNum = pageNum
                }
                this.$request.get('/enroll/selectByFitPage', {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        title: this.title,
                        sign: this.sign,
                        teacher: this.teacher,
                    }
                }).then(res => {
                    this.tableData = res.data.records
                    console.log(this.tableData)
                    this.total = res.data.total
                })
                //接收竞赛类型值
                this.$request.get('/sign/selectAll').then(res => {
                    this.options = res.data
                })
                //接收教师值
                this.$request.get('/user/selectTeacher').then(res => {
                    this.teachers = res.data
                    console.log(this.teachers)
                })
            },
            handleCurrentChange(pageNum) {
                this.load(pageNum)
            },
        }
    }

</script>

<style>
    .el-tooltip__popper {
        max-width: 400px;
    }
</style>